package com.company.crm.workbench.web.controller;

import com.company.crm.commons.constants.Constants;
import com.company.crm.commons.domain.ReturnObject;
import com.company.crm.settings.domain.DicValue;
import com.company.crm.settings.domain.User;
import com.company.crm.settings.service.DicValueService;
import com.company.crm.settings.service.UserService;
import com.company.crm.workbench.domain.Customer;
import com.company.crm.workbench.domain.Tran;
import com.company.crm.workbench.domain.TranHistory;
import com.company.crm.workbench.mapper.CustomerMapper;
import com.company.crm.workbench.mapper.TranHistoryMapper;
import com.company.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * 作者：王磊栋
 * 2021/4/25
 */
@Controller
public class TransactionController {
    @Autowired
    private UserService userService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private TranService tranService;
    @Autowired
    private TranHistoryMapper tranHistoryMapper;
    @RequestMapping("workbench/transaction/index.do")
    public String skipTransactionPage(Model model){
        List<DicValue> stageList = dicValueService.selectDicValueByCodeType("stage");
        model.addAttribute("stageList",stageList);
        List<DicValue> sourceList = dicValueService.selectDicValueByCodeType("source");
        model.addAttribute("sourceList",sourceList);

        List<DicValue> transactionTypeList = dicValueService.selectDicValueByCodeType("transactionType");
        model.addAttribute("transactionTypeList",transactionTypeList);
        return "workbench/transaction/index";
    }
    @RequestMapping("/workbench/transaction/createTran.do")
    public String createTran(Model model){
        List<User> userList = userService.selectAll();
        model.addAttribute("userList", userList);
        List<DicValue> stageList = dicValueService.selectDicValueByCodeType("stage");
        model.addAttribute("stageList",stageList);
        List<DicValue> sourceList = dicValueService.selectDicValueByCodeType("source");
        model.addAttribute("sourceList",sourceList);
        List<DicValue> transactionTypeList = dicValueService.selectDicValueByCodeType("transactionType");
        model.addAttribute("transactionTypeList",transactionTypeList);
        return "workbench/transaction/save";
    }

    @RequestMapping("/workbench/transaction/getPossibilityByStageValue.do")
    @ResponseBody
    public Object getPossibilityByStageValue(String stageValue){
        //通过读取配置文件获得这个stageValue
        ResourceBundle rb = ResourceBundle.getBundle("stageValue");
        String possibility = rb.getString(stageValue);
        return possibility;
    }
    //根据like客户名字查询
    @RequestMapping("/workbench/transaction/queryCustomerByName.do")
    @ResponseBody
    public Object queryCustomerByName(String customerName){
        List<Customer> customerList = customerMapper.selectCustomerByName(customerName);
        return customerList;
    }

    /*owner:owner,
     money:money,
     name:name,
     expectedDate:expectedDate,
     customerName:customerName,
     customerId:customerId,
     stage:stage,
     type:type,
     source:source,
     activityId:activityId,
     contactsId:contactsId,
     description:description,
     contactSummary:contactSummary,
     nextContactTime:nextContactTime*/
    @RequestMapping("/workbench/transaction/saveCreateTran.do")
    @ResponseBody
    public Object saveCreateTran(Tran tran,String customerName,HttpSession session){
        User user= (User) session.getAttribute(Constants.SESSION_USER);
        Map<String,Object> map = new HashMap<>();
        map.put("user", user);
        map.put("customerName", customerName);
        map.put("tran", tran);
        ReturnObject ro = new ReturnObject();
        try {
            tranService.saveCreateTran(map);
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            ro.setCode(Constants.LOGIN_CODE_FAIL);
        }
        return ro;
    }

    @RequestMapping("/workbench/transaction/detailTran.do")
    public String detailTran(String id,Model model){
        Tran tran = tranService.selectTranDetailByTranId(id);
        ResourceBundle rb = ResourceBundle.getBundle("stageValue");
        String possibility = rb.getString(tran.getStage());
        tran.setPossibility(possibility);
        List<DicValue> stageList = dicValueService.selectDicValueByCodeType("stage");
        List<TranHistory> tranHistoryList = tranHistoryMapper.selectTranHistoryForDetailByTranId(id);
        model.addAttribute("tran",tran);
        model.addAttribute("stageList",stageList);
        model.addAttribute("tranHistoryList",tranHistoryList);
        TranHistory tranHistory = null;
        for (int i = tranHistoryList.size()-1; i >=0 ; i--) {
            tranHistory =  tranHistoryList.get(i);
            break;
        }
        model.addAttribute("theOrderNo",tranHistory.getOrderNo());
        return "workbench/transaction/detail";
    }

}
